package brokers;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import models.SLA;

import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.SimEvent;

import util.Globals;

public class HOLBroker extends SkeletonBroker {



	public HOLBroker(String name, double endSimulationTime, SLA sla1, SLA sla2) throws Exception {
		super(name, endSimulationTime, sla1, sla2);
		priority1 = getGSLAFromFile(sla1);
		priority2 = getGSLAFromFile(sla2);
	}


	public static double getGSLAFromFile(SLA sla){

		double gSla = - 1;
		BufferedReader bufferedReader = null;
		ArrayList<String> p = new ArrayList<String>();
		String line = null;
		int j = 0;
		String[] tokens = null;

		try {
			bufferedReader = new BufferedReader(new FileReader("gslas/gslas2.855.5.csv"));
			while ((line = bufferedReader.readLine()) != null) {
				p.add(line);
				//System.out.println(p.get(j));
				j++;
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}catch (IOException e1) {
			e1.printStackTrace();
		}

		for(int y = 0; y < p.size(); y++){			
			if(p.get(y).contains(sla.toString())){
				tokens = p.get(y).split("\t");
				gSla = Double.parseDouble(tokens[1]);
				break;
			}
		}

		System.out.println("ZLA" + sla + " " + gSla);
		return gSla;
	}


	/**
	 * Process a cloudlet return event.
	 *
	 * @param ev a SimEvent object
	 *
	 * @pre ev != $null
	 * @post $none
	 */
	@Override
	protected void processCloudletReturn(SimEvent ev) {

		super.processCloudletReturn(ev);

		if (mycloudletList.size()>0 && CloudSim.clock() < endSimulationTime && returned < Globals.SIMULATION_STABILIZATION_CLOUDLETS + Globals.SIMULATION_CLOUDLETS) {
			submitCloudlets();
		}
	}


}

